package juc.day02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
编写一个程序，开启三个线程，这三个县城的ID分别为A、B、C，每个线程将自己的ID在屏幕上打印10遍，
要求输出的结果必须按顺序西显示，如：ABCABCABC...依次递归
 */
public class TestABCAlternate {

    public static void main(String[] args) {
        AlternateDemo alter = new AlternateDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 1; j <= 20; j++) {
                    alter.loopA(j);
                }
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 1; j <= 20; j++) {
                    alter.loopB(j);
                }
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 1; j <= 20; j++) {
                    alter.loopC(j);
                    System.out.println("-------------------");
                }
            }
        }, "C").start();
    }
}


class AlternateDemo {


    private int number = 1;//当前正在执行线程的标记

    private Lock lock = new ReentrantLock();

    private Condition con1 = lock.newCondition();
    private Condition con2 = lock.newCondition();
    private Condition con3 = lock.newCondition();

    /*
    循环几轮的表示
     */
    public void loopA(int totalLoop) {
        lock.lock();
        try {
            //判断当前线程执行标记是不是1，是1就执行
            if (number != 1) {
                con1.await();
            }
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
            }
            //当此线程打印完，唤醒别的线程
            number = 2;
            con2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void loopB(int totalLoop) {
        lock.lock();
        try {
            //判断当前线程执行标记是不是1，是1就执行
            if (number != 2) {
                con2.await();
            }
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
            }
            //当此线程打印完，唤醒别的线程
            number = 3;
            con3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void loopC(int totalLoop) {
        lock.lock();
        try {
            //判断当前线程执行标记是不是1，是1就执行
            if (number != 3) {
                con3.await();
            }
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
            }
            //当此线程打印完，唤醒别的线程
            number = 1;
            con1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
